package net.huashitong.synthetical.TaskJob.Service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.sys.util.RedisUtils;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import com.sun.mail.util.BEncoderStream;
import net.huashitong.synthetical.AddressBook.entity.AddressBook;
import net.huashitong.synthetical.AddressBook.filter.BookFilter;
import net.huashitong.synthetical.AddressBook.service.AddressBookService;
import net.huashitong.synthetical.TaskJob.Entity.TaskJob;
import net.huashitong.synthetical.TaskJob.Entity.TaskProcess;
import net.huashitong.synthetical.TaskJob.dao.TaskProcessDao;
import net.huashitong.synthetical.TaskJob.filter.TaskProcessFilter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TaskProcessService extends StringPKService<TaskProcess> {
    @Autowired
    private TaskProcessDao dao;

    @Autowired
    private SysTableService sysTableService;


    @Autowired
    private TaskJobService taskJobService;

    @Autowired
    private AddressBookService bookService;

    @Override
    protected BaseDao<TaskProcess, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(TaskProcess obj) {
        obj.setSysId(sysTableService.updateMaxSysId("t_task_process",null));
    }

    /**
     *获取过程节点
     * @param filter
     * @return
     */
    public List<TaskProcess> getProcessList(TaskProcessFilter filter)
    {
        return dao.getprocessList(filter);
    }

    /**
     * 保存节点
     * @param filter
     */
    public void saveProcessJob(TaskProcessFilter filter)
    {
        //保存立项方 节点
        TaskProcess taskProcess = new TaskProcess();
       // BeanUtils.copyProperties(filter,taskProcess);

        taskProcess.setState("0"); //未签收
        taskProcess.setNodeHandle("0"); //该节点是否需要执行任务
        taskProcess.setNodeResult("0");
        taskProcess.setTaskId(filter.getTaskId());
        String deptId = SysUserUtils.getDeptId();
        SysDept  dept = RedisUtils.getDept(deptId);
        String username =SysUserUtils.getSysPrincipal().getName();
        taskProcess.setRespDept(dept.getDeptName());
        taskProcess.setRespCode(deptId);
        taskProcess.setRespUser(username);
        taskProcess.setNodeId(1);
        taskProcess.setNodestage("立项");
        save(taskProcess);

        //保存 承办人 节点
        TaskProcess taskProcess2 = new TaskProcess();
        BeanUtils.copyProperties(filter,taskProcess2);
        taskProcess2.setState("0"); //未签收
        taskProcess2.setNodeHandle("1"); //该节点是否需要执行任务
        taskProcess2.setNodeResult("0");
        taskProcess2.setRespUser(filter.getRespDept());
        taskProcess2.setNodeId(2);
        taskProcess2.setNodestage("承办");
        save(taskProcess2);
    }

    public void saveOrUpdateJob(TaskProcessFilter filter)
    {

        TaskJob job = taskJobService.getById(filter.getTaskId());


        //承办人
         if (ObjectUtils.isNotBlank( job))
        {
            job.setResUser( filter.getRespUser());
           // job.setEndorseDept(filter.getEndorseCode());
            BookFilter bf  =new BookFilter();
            bf.setDeptCode(filter.getEndorseDept());

            AddressBook book  = bookService.getBookObj(bf);
            //job.setRespCode(ObjectUtils.isNotBlank(book)?book.getDeptCode():null);
           // job.setResDept(ObjectUtils.isNotBlank(book)?book.getDeptName():null);

            job.setEndorseDept(ObjectUtils.isNotBlank(book)?book.getDeptName():null);
            job.setEndorseCode(filter.getEndorseDept());
            job.setEndorseUser(filter.getEndorseUser());
            if("on".equals(filter.getIsScore()))
            {
                job.setIsScore(1);
            }else {
                job.setIsScore(0);
            }
            if("on".equals(filter.getTaskResult())) {
                job.setTaskResult("1");
            }else
            {
                job.setTaskResult("0");
            }

            taskJobService.update(job);
        }
        if(StringUtils.isNotBlank(job.getResUser()))
        {
            TaskProcess proces  = new TaskProcess();
            //BeanUtils.copyProperties(filter,proces);
            proces.setRespDept(job.getResDept());
            proces.setRespCode(job.getRespCode());
            proces.setRespUser(job.getResUser());
            proces.setTaskId(filter.getTaskId());
            proces.setNodeId(3);
            proces.setNodeResult("0");
            proces.setState("0");
            proces.setNodestage("承办");
            save(proces);
        }
        if(StringUtils.isNotBlank(job.getEndorseCode()))
        {
            TaskProcess proces  = new TaskProcess();
           // BeanUtils.copyProperties(filter,proces);
            proces.setRespDept(job.getEndorseDept());
            proces.setRespCode(job.getEndorseCode());
            proces.setRespUser(job.getEndorseUser());
            proces.setTaskId(filter.getTaskId());
            proces.setNodeId(4);
            proces.setNodeResult("0");
            proces.setState("0");
            proces.setNodestage("批转");
            save(proces);
        }


       /* String[] nodeids ={"3","4"};
        for (String item :nodeids)
        {
            filter.setNodeid(item);
            List<TaskProcess> processList = this.getProcessList(filter);
            if(ObjectUtils.isBlank(processList))
            {
                TaskProcess proces  = new TaskProcess();
                BeanUtils.copyProperties(filter,proces);
                proces.setNodeId(2);
                proces.setNodeResult("0");
                proces.setState("0");
                save(proces);
            }
            else
            {
                TaskProcess proces =  processList.get(0);
                BeanUtils.copyProperties(filter,proces);
                proces.setNodeId(2);
                update(proces);
            }
        }
*/



    }


}
